/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frm_JInterFrameTraCuuSach.java
 *
 * Created on Aug 2, 2011, 6:08:42 PM
 */
package gui;


import dao.SachDAO;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import pojo.SachPOJO;

/**
 *
 * @author HP
 */
public class frm_JInterFrameTraCuuSach extends javax.swing.JInternalFrame {

    ArrayList<SachPOJO> dssach = new ArrayList<SachPOJO>();
    SachDAO sdao = new SachDAO();
    private DefaultTableModel tablemodelSach;

    /** Creates new form frm_JInterFrameTraCuuSach */
    private void inittable() {

        tablemodelSach = new DefaultTableModel();
        this.tablemodelSach.addColumn("STT");
        this.tablemodelSach.addColumn("Ma Sach");
        this.tablemodelSach.addColumn("Ten Sach");
        this.tablemodelSach.addColumn("Gia Ban");

    }

    public frm_JInterFrameTraCuuSach() {
        initComponents();
        inittable();
        this.jtblDSSach.setModel(tablemodelSach);
         this.jtxtGiaDen.enable(false);//textbox khong duoc thay doi gia tri khi click chuot
         this.jtxtGiaTu.enable(false);
    }

    private void HienThiDanhSachSach(String _tenSach) {
        dssach = sdao.timKiemSach_TheoTenSach(_tenSach);
        inittable();


        int t = 1;
        for (SachPOJO i : dssach) {
            Vector vt = new Vector();
            vt.add(t);
            vt.add(i.getMaSach());
            vt.add(i.getTenSach());
            vt.add(i.getGiaBan());
            tablemodelSach.addRow(vt);
            t++;
        }
        this.jtblDSSach.setModel(tablemodelSach);
    }

    private void HienThiDanhSachSach_GiaTu_GiaDen(float _giatu, float _giaden) {
        dssach = sdao.timKiemSach_TheoGiaTu_GiaDen(_giatu, _giaden);
        inittable();


        int t = 1;
        for (SachPOJO i : dssach) {
            Vector vt = new Vector();
            vt.add(t);
            vt.add(i.getMaSach());
            vt.add(i.getTenSach());
            vt.add(i.getGiaBan());
            tablemodelSach.addRow(vt);
            t++;
        }
        this.jtblDSSach.setModel(tablemodelSach);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jtxtTenSach = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jbtnTimKiem = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jchbxTimKiemTheoGiaSach = new javax.swing.JCheckBox();
        jtxtGiaDen = new javax.swing.JTextField();
        jtxtGiaTu = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtblDSSach = new javax.swing.JTable();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Tra Cuu Sach");

        jPanel1.setBackground(new java.awt.Color(255, 204, 204));

        jPanel2.setBackground(new java.awt.Color(204, 204, 255));

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabel3.setText("Tìm Kiếm Sách");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(339, 339, 339)
                .addComponent(jLabel3)
                .addContainerGap(448, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel3)
                .addContainerGap(10, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(51, 51, 255), 2), "Thong Tin Tim Kiem"));

        jLabel1.setText("Ten Sach");

        jtxtTenSach.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                jtxtTenSachCaretUpdate(evt);
            }
        });

        jLabel2.setText("Giá Từ:");

        jbtnTimKiem.setText("Tim Kiem");
        jbtnTimKiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnTimKiemActionPerformed(evt);
            }
        });

        jLabel4.setText("Giá Đến:");

        jchbxTimKiemTheoGiaSach.setText("Tìm Kiếm Sách Theo Giá Bán");
        jchbxTimKiemTheoGiaSach.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jchbxTimKiemTheoGiaSachActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(jchbxTimKiemTheoGiaSach, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jbtnTimKiem, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel5Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(18, 18, 18)
                                .addComponent(jtxtTenSach, javax.swing.GroupLayout.DEFAULT_SIZE, 236, Short.MAX_VALUE))
                            .addGroup(jPanel5Layout.createSequentialGroup()
                                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel4))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jtxtGiaTu, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
                                    .addComponent(jtxtGiaDen, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE))))
                        .addGap(35, 35, 35))))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtxtTenSach, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(16, 16, 16)
                .addComponent(jchbxTimKiemTheoGiaSach)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jtxtGiaTu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jtxtGiaDen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
                .addComponent(jbtnTimKiem, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28))
        );

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(203, Short.MAX_VALUE))
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 51, 204), 2), "Danh Sach Sach"));

        jtblDSSach.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jtblDSSach);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 527, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 438, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(23, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnTimKiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnTimKiemActionPerformed
        // TODO add your handling code here:
        try {
            String tensach = jtxtTenSach.getText();
            if(jchbxTimKiemTheoGiaSach.isSelected()==true)
            {
            float giatu = Float.parseFloat(jtxtGiaTu.getText());
            float giaden = Float.parseFloat(jtxtGiaDen.getText());
            HienThiDanhSachSach_GiaTu_GiaDen(giatu, giaden);
            }
            else
            {
                HienThiDanhSachSach(tensach);
            }
        } catch (Exception ex) {
            
        }
    }//GEN-LAST:event_jbtnTimKiemActionPerformed

    private void jtxtTenSachCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_jtxtTenSachCaretUpdate
        // TODO add your handling code here:
        try {

            String tensach = jtxtTenSach.getText();
            // float gia=Float.parseFloat(jtxtGiaBan.getText());
            HienThiDanhSachSach(tensach);
        } catch (Exception ex) {
            // HienThiDanhSachSach("");
        }
    }//GEN-LAST:event_jtxtTenSachCaretUpdate

    private void jchbxTimKiemTheoGiaSachActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jchbxTimKiemTheoGiaSachActionPerformed
        // TODO add your handling code here:
        if (this.jchbxTimKiemTheoGiaSach.isSelected() == true) {
            this.jtxtGiaDen.enable(true);
            this.jtxtGiaTu.enable(true);
        } else {
            this.jtxtGiaDen.enable(false);
            this.jtxtGiaTu.enable(false);
        }
    }//GEN-LAST:event_jchbxTimKiemTheoGiaSachActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtnTimKiem;
    private javax.swing.JCheckBox jchbxTimKiemTheoGiaSach;
    private javax.swing.JTable jtblDSSach;
    private javax.swing.JTextField jtxtGiaDen;
    private javax.swing.JTextField jtxtGiaTu;
    private javax.swing.JTextField jtxtTenSach;
    // End of variables declaration//GEN-END:variables
}
